home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / zip.zip / DIR_OS2.C < prev    next >
C/C++ Source or Header  |  1992-09-22  |  6KB  |  293 lines

  1. /*
  2.  * @(#)dir.c 1.4 87/11/06 Public Domain.
  3.  *
  4.  *  A public domain implementation of BSD directory routines for
  5.  *  MS-DOS.  Written by Michael Rendell ({uunet,utai}michael@garfield),
  6.  *  August 1897
  7.  *  Ported to OS/2 by Kai Uwe Rommel
  8.  *  December 1989, February 1990
  9.  *  Change for HPFS support, October 1990
  10.  */
  11.  
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <malloc.h>
  18. #include <string.h>
  19. #include <ctype.h>
  20.  
  21. #define INCL_NOPM
  22. #include <os2.h>
  23.  
  24. #include "dir_os2.h"
  25.  
  26.  
  27. int attributes = A_DIR | A_HIDDEN;
  28.  
  29.  
  30. static char *getdirent(char *);
  31. static void free_dircontents(struct _dircontents *);
  32.  
  33. static HDIR hdir;
  34. static USHORT count;
  35. static FILEFINDBUF find;
  36.  
  37.  
  38. DIR *opendir(char *name)
  39. {
  40.   struct stat statb;
  41.   DIR *dirp;
  42.   char c;
  43.   char *s;
  44.   struct _dircontents *dp;
  45.   char nbuf[MAXPATHLEN + 1];
  46.   int len;
  47.  
  48.   strcpy(nbuf, name);
  49.   len = strlen (nbuf);
  50.   s = nbuf + len;
  51.  
  52. #if 1
  53.   if ( ((c = nbuf[strlen(nbuf) - 1]) == '\\' || c == '/') &&
  54.        (strlen(nbuf) > 1) )
  55.   {
  56.     nbuf[strlen(nbuf) - 1] = 0;
  57.  
  58.     if ( nbuf[strlen(nbuf) - 1] == ':' )
  59.       strcat(nbuf, "\\.");
  60.   }
  61.   else
  62.     if ( nbuf[strlen(nbuf) - 1] == ':' )
  63.       strcat(nbuf, ".");
  64. #else
  65.   if ( len && ((c = nbuf[len-1]) == '\\' || c == '/' || c == ':') )
  66.   {
  67.     nbuf[len++] = '.';      /* s now points to '.' */
  68.     nbuf[len] = 0;
  69.   }
  70. #endif
  71.  
  72.   if (stat(nbuf, &statb) < 0 || (statb.st_mode & S_IFMT) != S_IFDIR)
  73.     return NULL;
  74.  
  75.   if ( (dirp = malloc(sizeof(DIR))) == NULL )
  76.     return NULL;
  77.  
  78. #if 1
  79.   if ( nbuf[strlen(nbuf) - 1] == '.' )
  80.     strcpy(nbuf + strlen(nbuf) - 1, "*.*");
  81.   else
  82.     if ( ((c = nbuf[strlen(nbuf) - 1]) == '\\' || c == '/') &&
  83.          (strlen(nbuf) == 1) )
  84.       strcat(nbuf, "*.*");
  85.     else
  86.       strcat(nbuf, "\\*.*");
  87. #else
  88.   if ( *s == 0 )
  89.     *s++ = '\\';
  90.  
  91.   strcpy (s, "*.*");
  92. #endif
  93.  
  94.   dirp -> dd_loc = 0;
  95.   dirp -> dd_contents = dirp -> dd_cp = NULL;
  96.  
  97.   if ((s = getdirent(nbuf)) == NULL)
  98.     return dirp;
  99.  
  100.   do
  101.   {
  102.     if (((dp = malloc(sizeof(struct _dircontents))) == NULL) ||
  103.         ((dp -> _d_entry = malloc(strlen(s) + 1)) == NULL)      )
  104.     {
  105.       if (dp)
  106.         free(dp);
  107.       free_dircontents(dirp -> dd_contents);
  108.  
  109.       return NULL;
  110.     }
  111.  
  112.     if (dirp -> dd_contents)
  113.     {
  114.       dirp -> dd_cp -> _d_next = dp;
  115.       dirp -> dd_cp = dirp -> dd_cp -> _d_next;
  116.     }
  117.     else
  118.       dirp -> dd_contents = dirp -> dd_cp = dp;
  119.  
  120.     strcpy(dp -> _d_entry, s);
  121.     dp -> _d_next = NULL;
  122.  
  123.     dp -> _d_size = find.cbFile;
  124.     dp -> _d_mode = find.attrFile;
  125.     dp -> _d_time = *(unsigned *) &(find.ftimeLastWrite);
  126.     dp -> _d_date = *(unsigned *) &(find.fdateLastWrite);
  127.   }
  128.   while ((s = getdirent(NULL)) != NULL);
  129.  
  130.   dirp -> dd_cp = dirp -> dd_contents;
  131.  
  132.   return dirp;
  133. }
  134.  
  135.  
  136. void closedir(DIR * dirp)
  137. {
  138.   free_dircontents(dirp -> dd_contents);
  139.   free(dirp);
  140. }
  141.  
  142.  
  143. struct direct *readdir(DIR * dirp)
  144. {
  145.   static struct direct dp;
  146.  
  147.   if (dirp -> dd_cp == NULL)
  148.     return NULL;
  149.  
  150.   dp.d_namlen = dp.d_reclen =
  151.     strlen(strcpy(dp.d_name, dirp -> dd_cp -> _d_entry));
  152.  
  153.   dp.d_ino = 0;
  154.  
  155.   dp.d_size = dirp -> dd_cp -> _d_size;
  156.   dp.d_mode = dirp -> dd_cp -> _d_mode;
  157.   dp.d_time = dirp -> dd_cp -> _d_time;
  158.   dp.d_date = dirp -> dd_cp -> _d_date;
  159.  
  160.   dirp -> dd_cp = dirp -> dd_cp -> _d_next;
  161.   dirp -> dd_loc++;
  162.  
  163.   return &dp;
  164. }
  165.  
  166.  
  167. void seekdir(DIR * dirp, long off)
  168. {
  169.   long i = off;
  170.   struct _dircontents *dp;
  171.  
  172.   if (off >= 0)
  173.   {
  174.     for (dp = dirp -> dd_contents; --i >= 0 && dp; dp = dp -> _d_next);
  175.  
  176.     dirp -> dd_loc = off - (i + 1);
  177.     dirp -> dd_cp = dp;
  178.   }
  179. }
  180.  
  181.  
  182. long telldir(DIR * dirp)
  183. {
  184.   return dirp -> dd_loc;
  185. }
  186.  
  187.  
  188. static void free_dircontents(struct _dircontents * dp)
  189. {
  190.   struct _dircontents *odp;
  191.  
  192.   while (dp)
  193.   {
  194.     if (dp -> _d_entry)
  195.       free(dp -> _d_entry);
  196.  
  197.     dp = (odp = dp) -> _d_next;
  198.     free(odp);
  199.   }
  200. }
  201.  
  202.  
  203. static char *getdirent(char *dir)
  204. {
  205.   int done;
  206.  
  207.   if (dir != NULL)
  208.   {                                    /* get first entry */
  209.     hdir = HDIR_CREATE;
  210.     count = 1;
  211.     done = DosFindFirst(dir, &hdir, attributes,
  212.                         &find, sizeof(find), &count, 0L);
  213.   }
  214.   else                                 /* get next entry */
  215.     done = DosFindNext(hdir, &find, sizeof(find), &count);
  216.  
  217.   if (done == 0)
  218.     return find.achName;
  219.   else
  220.   {
  221.     DosFindClose(hdir);
  222.     return NULL;
  223.   }
  224. }
  225.  
  226.  
  227. /* ISFAT.C
  228.  *
  229.  * Autor:    Kai Uwe Rommel
  230.  * Datum:    Sun 28-Oct-1990
  231.  *
  232.  * Compiler: MS C ab 6.00
  233.  * System:   OS/2 ab 1.2
  234.  */
  235.  
  236. #define LABEL    "isfat.c"
  237. #define VERSION  "1.0"
  238.  
  239.  
  240. /* #include <stdio.h>
  241. #include <stdlib.h>
  242. #include <string.h>
  243. #include <ctype.h>
  244.  
  245. #define INCL_NOPM
  246. #include <os2.h> */
  247.  
  248.  
  249. int IsFileSystemFAT(char *dir)
  250. {
  251.   USHORT nDrive;
  252.   ULONG lMap;
  253.   BYTE bData[64], bName[3];
  254.   USHORT cbData;
  255.   static USHORT nLastDrive = -1, nResult;
  256.  
  257.   if ( _osmode == DOS_MODE )
  258.     return TRUE;
  259.   else
  260.   {
  261.     /* We separate FAT and HPFS+other file systems here.
  262.        at the moment I consider other systems to be similar to HPFS,
  263.        i.e. support long file names and beeing case sensitive */
  264.  
  265.     if ( isalpha(dir[0]) && (dir[1] == ':') )
  266.       nDrive = toupper(dir[0]) - '@';
  267.     else
  268.       DosQCurDisk(&nDrive, &lMap);
  269.  
  270.     if ( nDrive == nLastDrive )
  271.       return nResult;
  272.  
  273.     bName[0] = (char) (nDrive + '@');
  274.     bName[1] = ':';
  275.     bName[2] = 0;
  276.  
  277.     nLastDrive = nDrive;
  278.     cbData = sizeof(bData);
  279.  
  280.     if ( !DosQFSAttach(bName, 0U, 1U, bData, &cbData, 0L) )
  281.       nResult = !strcmp(bData + (*(USHORT *) (bData + 2) + 7), "FAT");
  282.     else
  283.       nResult = FALSE;
  284.  
  285.     /* End of this ugly code */
  286.     return nResult;
  287.   }
  288. }
  289.  
  290.  
  291.  
  292. /* End of ISFAT.C */
  293.